<?php
//铃盘搜索索引
/**
 * ring_news.state
 *  0 未加入到队列（获取标签队列）
 *  1 已加入到队列
 *  2 队列已执行
 *  3 已加入到索引
 *  9 未加入到索引
 */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

include_once('../lib/config.php');
include_once('../lib/Db.class.php');
include_once('../lib/Model.class.php');
include_once('../lib/Upyun.class.php');
include_once('../lib/function.php');
include_once('/usr/local/xunsearch/sdk/php/lib/XS.php');

$redis = new Redis();  
$ret = $redis->connect("localhost","6379");  //php客户端设置的ip及端口
if(!$ret) die('redis connction faild');
$redis->auth(REDIS_PASS);
$redis->select(1);

$GLOBALS['config']['master']['db_database'] = 'mh_lnpan';
$GLOBALS['config']['slave']['db_database']  = 'mh_lnpan';

$model = new Db;

$queue_ids_key = 'LNPAN_INDEX_QUEQUE_IDS';
$queue_ids = array();

$loop = 0;
while (true) {
  $stop = $redis->get('LNPAN_QUEUE_STOP');
  if($stop) {
    die('stop');
  }

  $where = 'state=2 ';
  $queue_ids = $redis->hKeys($queue_ids_key);
  if($queue_ids) {
	 //$where .= 'AND id not in('.implode(',', $queue_ids).') ';
  }
  $sql  = 'select id,name,addtime,type_id from ring_news where '.$where.' order by id asc limit 50'; 
  $list = $model->query($sql);
  $model->close();
  if(count($list) == 0) die('empty');
  
  foreach ($list as $item) {
	 $redis->hSet($queue_ids_key, $item['id'], date('Y-m-d H:i:s'));
  }
  
  foreach ($list as $item) {
    $type = $model->find('ring_type', "id='$item[type_id]'");
    $singers = $model->query("select s.name from ring_news_singer ns left join ring_singer s on s.id=ns.singer_id where ns.news_id='".$item['id']."'");
    $tags = $model->query("select t.name from ring_news_tag nt left join ring_tag t on t.id=nt.tag_id where nt.news_id='".$item['id']."'");
    $formats = $model->query("select format_size,name from ring_news_format a left join ring_format b on a.`format_id`=b.`id` where a.`news_id`='".$item['id']."'");

    $index_singers = $index_tags = $index_formats = array();

    foreach ($singers as $singer) {
      $index_singers[] = $singer['name'];
    }
    foreach ($tags as $tag) {
      $index_tags[] = $tag['name'];
    }
    foreach ($formats as $format) {
      $index_formats[] = $format['name'];
    }
    $index_data = array(
      'id' => $item['id'],
      'title' => $item['name'],
      'content' => implode(',', $index_singers),
      'tags' => implode(',', $index_tags),
      'addtime' => $item['addtime'],
      'type' => $type['name'],
      'formats' => implode(',', $index_formats),
      'size' => $formats[0]['format_size'],
    );

    // 创建文档对象
    $doc = new XSDocument;
    $doc->setFields($index_data);

    // 更新到索引数据库中
    try{  
	$xs = new XS('lnpan');
        $index = $xs->index; 
    } catch(Exception $e) {
      print $e->getMessage();   
      exit();
    }
    $result = $index->update($doc);
  
    if($result) {
        $model->query('update ring_news set state=3 where id='.$item['id']);
      } else {
  	  $model->query('update ring_news set state=9 where id='.$item['id']);
  	}
  	
  	$redis->hDel($queue_ids_key, $item['id']);
  }

  $model->close();
  
  $xs = new XS('lnpan');
  $index = $xs->index;
  $index->flushIndex();

  $search = $xs->search; //获取搜索对象
  $count = $search->getDbTotal();
 
  var_dump($count); 
  $redis->lPush('tag_index_count', 'Time:'.date('Y-m-d H:i:s').'||'.$count);
  $loop++;
break;
  if($loop > 100) die();
  sleep(1);
}



